package org.ala.tiktools.generator.mybatis.config.info;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ala
 * @date 2024-12-30 13:41
 */
public class Part {
    protected String name;
    protected String database;
    protected String[] tables;
    protected String charset;
    protected boolean shardTable;
    protected Author author;
    protected Swagger swagger;
    protected Out out;


    public static Part build(String name, Map<String, Object> kvs) {
        Part part = new Part();
        part.name = name;
        part.setDatabase((String)kvs.get("database"));
        part.setTables(Arrays.stream(kvs.get("tables").toString().split(",")).map(s -> s.trim()).collect(Collectors.toList()).toArray(new String[0]));
        part.setCharset((String)kvs.get("charset"));
        part.setShardTable((Boolean) kvs.getOrDefault("shardTable", false));
        part.setAuthor(Author.build((Map<String, Object>) kvs.get("author")));
        part.setSwagger(Swagger.build((Map<String, Object>) kvs.get("swagger")));
        part.setOut(Out.build((Map<String, Object>) kvs.get("out")));
        return part;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDatabase() {
        return database;
    }

    public void setDatabase(String database) {
        this.database = database;
    }

    public String[] getTables() {
        return tables;
    }

    public void setTables(String[] tables) {
        this.tables = tables;
    }

    public String getCharset() {
        return charset;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }

    public Author getAuthor() {
        return author;
    }

    public void setAuthor(Author author) {
        this.author = author;
    }

    public Swagger getSwagger() {
        return swagger;
    }

    public void setSwagger(Swagger swagger) {
        this.swagger = swagger;
    }

    public Out getOut() {
        return out;
    }

    public void setOut(Out out) {
        this.out = out;
    }

    public boolean isShardTable() {
        return shardTable;
    }

    public void setShardTable(boolean shardTable) {
        this.shardTable = shardTable;
    }

    /**
     *  作者信息
     */
    public static class Author {
        protected String name;
        protected String english;

        public static Author build(Map<String, Object> kvs) {
            Author author = new Author();
            author.setName((String)kvs.get("name"));
            author.setEnglish((String)kvs.get("english"));
            return author;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getEnglish() {
            return english;
        }

        public void setEnglish(String english) {
            this.english = english;
        }
    }
    /**
     *  swagger
     */
    public static class Swagger {
        protected boolean enabled;

        public static Swagger build(Map<String, Object> kvs) {
            Swagger swagger = new Swagger();
            swagger.setEnabled((Boolean) kvs.getOrDefault("enabled", false));
            return swagger;
        }

        public boolean isEnabled() {
            return enabled;
        }

        public void setEnabled(boolean enable) {
            this.enabled = enable;
        }
    }
    /**
     *  输出信息
     */
    public static class Out {
        protected Entity entity;
        protected Query query;
        protected BatchUpdate batchUpdate;
        protected Mapper mapper;
        protected Dao dao;
        protected Enum enumInfo;
        protected OrderBy orderBy;
        protected Shard shard;

        public static Out build(Map<String, Object> kvs) {
            Out out = new Out();
            out.setEntity(Entity.build((Map<String, Object>) kvs.get("entity")));
            out.setQuery(Query.build((Map<String, Object>) kvs.get("query")));
//            out.setBatchUpdate(BatchUpdate.build((Map<String, Object>) kvs.get("batchUpdate")));
            out.setMapper(Mapper.build((Map<String, Object>) kvs.get("mapper")));
            out.setDao(Dao.build((Map<String, Object>) kvs.get("dao")));
//            out.setEnumInfo(Enum.build((Map<String, Object>) kvs.get("enum")));
            out.setOrderBy(OrderBy.build((Map<String, Object>) kvs.get("orderBy")));
            out.setShard(Shard.build((Map<String, Object>) kvs.get("shard")));
            return out;
        }

        public Entity getEntity() {
            return entity;
        }
        public void setEntity(Entity entity) {
            this.entity = entity;
        }
        public Query getQuery() {
            return query;
        }
        public void setQuery(Query query) {
            this.query = query;
        }
        public BatchUpdate getBatchUpdate() {
            return batchUpdate;
        }
        public void setBatchUpdate(BatchUpdate batchUpdate) {
            this.batchUpdate = batchUpdate;
        }
        public Mapper getMapper() {
            return mapper;
        }
        public void setMapper(Mapper mapper) {
            this.mapper = mapper;
        }
        public Dao getDao() {
            return dao;
        }
        public void setDao(Dao dao) {
            this.dao = dao;
        }
        public Enum getEnumInfo() {
            return enumInfo;
        }
        public void setEnumInfo(Enum enumInfo) {
            this.enumInfo = enumInfo;
        }
        public OrderBy getOrderBy() {
            return orderBy;
        }
        public void setOrderBy(OrderBy orderBy) {
            this.orderBy = orderBy;
        }

        public Shard getShard() {
            return shard;
        }

        public void setShard(Shard shard) {
            this.shard = shard;
        }
    }
    public static class OutInfo {
        protected String pkg;
        protected String suffix;
        public String getPkg() {
            return pkg;
        }
        public void setPkg(String pkg) {
            this.pkg = pkg;
        }
        public String getSuffix() {
            return suffix;
        }
        public void setSuffix(String suffix) {
            this.suffix = suffix;
        }
    }
    public static class Entity extends OutInfo {
        public static Entity build(Map<String, Object> kvs) {
            Entity entity = new Entity();
            entity.setPkg((String)kvs.get("pkg"));
            entity.setSuffix((String)kvs.getOrDefault("suffix", "DO"));
            return entity;
        }
    }
    public static class Query  extends OutInfo {
        public static Query build(Map<String, Object> kvs) {
            Query entity = new Query();
            entity.setPkg((String)kvs.get("pkg"));
            entity.setSuffix((String)kvs.getOrDefault("suffix", "QO"));
            return entity;
        }
    }
    public static class BatchUpdate  extends OutInfo {
        public static BatchUpdate build(Map<String, Object> kvs) {
            BatchUpdate entity = new BatchUpdate();
            entity.setPkg((String)kvs.get("pkg"));
            entity.setSuffix((String)kvs.getOrDefault("suffix", "BU"));
            return entity;
        }
    }
    public static class Mapper  extends OutInfo {
        public static Mapper build(Map<String, Object> kvs) {
            Mapper entity = new Mapper();
            entity.setPkg((String)kvs.get("pkg"));
            entity.setSuffix((String)kvs.getOrDefault("suffix", "Mapper"));
            return entity;
        }
    }
    public static class Dao  extends OutInfo {
        protected String annotation;
        protected String pagement;
        protected String pager;
        protected String mutileUpdate;
        public static Dao build(Map<String, Object> kvs) {
            Dao entity = new Dao();
            entity.setPkg((String)kvs.get("pkg"));
            entity.setSuffix((String)kvs.getOrDefault("suffix", "Dao"));
            entity.setAnnotation((String)kvs.get("annotation"));
            entity.setPagement((String)kvs.get("pagement"));
            entity.setPager((String)kvs.get("pager"));
            entity.setMutileUpdate((String) kvs.get("mutileUpdate"));
            return entity;
        }
        public String getAnnotation() {
            return annotation;
        }
        public void setAnnotation(String annotation) {
            this.annotation = annotation;
        }
        public String getPagement() {
            return pagement;
        }
        public void setPagement(String pagement) {
            this.pagement = pagement;
        }
        public String getPager() {
            return pager;
        }
        public void setPager(String pager) {
            this.pager = pager;
        }
        public String getMutileUpdate() {
            return mutileUpdate;
        }
        public void setMutileUpdate(String mutileUpdate) {
            this.mutileUpdate = mutileUpdate;
        }
    }
    public static class Enum  extends OutInfo {
        protected String annotation;
        public static Enum build(Map<String, Object> kvs) {
            Enum entity = new Enum();
            entity.setPkg((String)kvs.get("pkg"));
            entity.setSuffix((String)kvs.getOrDefault("suffix", "Enums"));
            entity.setAnnotation((String)kvs.get("annotation"));
            return entity;
        }
        public String getAnnotation() {
            return annotation;
        }
        public void setAnnotation(String annotation) {
            this.annotation = annotation;
        }
    }
    public static class OrderBy extends OutInfo {
        protected String parent;
        protected String comparator;
        public static OrderBy build(Map<String, Object> kvs) {
            OrderBy entity = new OrderBy();
            entity.setPkg((String)kvs.get("pkg"));
            entity.setSuffix((String)kvs.getOrDefault("suffix", "OrderBy"));
            entity.setParent((String) kvs.get("parent"));
            entity.setComparator((String) kvs.get("comparator"));
            return entity;
        }
        public String getParent() {
            return parent;
        }
        public void setParent(String parent) {
            this.parent = parent;
        }

        public String getComparator() {
            return comparator;
        }

        public void setComparator(String comparator) {
            this.comparator = comparator;
        }
    }
    public static class Shard extends OutInfo {
        protected String annotationShard, annotationShardRouter, annotationShardTransaction;
        protected String router;
        protected String pagerCount, afterPager;
        protected String shardInfo;
        protected boolean shardAutoCreateTable;
        public static Shard build(Map<String, Object> kvs) {
            Shard entity = new Shard();
            entity.setPkg((String)kvs.get("pkg"));
//            entity.setSuffix((String)kvs.getOrDefault("suffix", "BU"));
            entity.annotationShard = (String) kvs.get("annotationShard");
            entity.annotationShardRouter = (String) kvs.get("annotationShardRouter");
            entity.annotationShardTransaction = (String) kvs.get("annotationShardTransaction");
            entity.router = (String) kvs.get("router");
            entity.pagerCount = (String) kvs.get("pagerCount");
            entity.afterPager = (String) kvs.get("afterPager");
            entity.shardInfo = (String) kvs.get("shardInfo");
            try {
                entity.shardAutoCreateTable = (Boolean) kvs.get("shardAutoCreateTable");
            } catch (Exception e) {
                e.printStackTrace();
            }

            return entity;
        }

        public String getAnnotationShard() {
            return annotationShard;
        }

        public void setAnnotationShard(String annotationShard) {
            this.annotationShard = annotationShard;
        }

        public String getAnnotationShardRouter() {
            return annotationShardRouter;
        }

        public void setAnnotationShardRouter(String annotationShardRouter) {
            this.annotationShardRouter = annotationShardRouter;
        }

        public String getAnnotationShardTransaction() {
            return annotationShardTransaction;
        }

        public void setAnnotationShardTransaction(String annotationShardTransaction) {
            this.annotationShardTransaction = annotationShardTransaction;
        }

        public String getRouter() {
            return router;
        }

        public void setRouter(String router) {
            this.router = router;
        }

        public String getPagerCount() {
            return pagerCount;
        }

        public void setPagerCount(String pagerCount) {
            this.pagerCount = pagerCount;
        }

        public String getAfterPager() {
            return afterPager;
        }

        public void setAfterPager(String afterPager) {
            this.afterPager = afterPager;
        }

        public String getShardInfo() {
            return shardInfo;
        }

        public void setShardInfo(String shardInfo) {
            this.shardInfo = shardInfo;
        }

        public boolean isShardAutoCreateTable() {
            return shardAutoCreateTable;
        }

        public void setShardAutoCreateTable(boolean shardAutoCreateTable) {
            this.shardAutoCreateTable = shardAutoCreateTable;
        }
    }
}
